package com.cn.transfer.jobhandler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvSaSaleOrder;
import com.cn.transfer.common.entity.VvSaSaleOrderB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.PurchaseOrder;
import com.cn.transfer.common.vo.PurchaseOrderDetail;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvSaSaleOrderBMapper;
import com.cn.transfer.mapper.VvSaSaleOrderMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 向伟典
 * @date: 2023/9/14 11:47
 * @Description: 把t+里面审核的数据传入到简道云
 */
@Slf4j
@Component
public class SaleOrderAddXxljob {

    @Autowired
    private VvSaSaleOrderMapper vvSaSaleOrderMapper;
    @Autowired
    private VvSaSaleOrderBMapper vvSaSaleOrderBMapper;

    @Transactional
    @XxlJob("SaleOrderAdd")
    public void saleOrderAdd() {
        String param = XxlJobHelper.getJobParam();
        List<VvSaSaleOrder> vvSaSaleOrdersList =null;
        if (param.equals("v")){
            vvSaSaleOrdersList  = vvSaSaleOrderMapper.saleOrderNotNullDayList();
        }else {
            vvSaSaleOrdersList  = vvSaSaleOrderMapper.saleOrderNotNullList();
        }
        Header[] originalHeaders = new Header[2];
        originalHeaders[0] = new BasicHeader("Authorization", "Bearer " + KeyConstant.JDY_KEY);
        originalHeaders[1] = new BasicHeader("Content-Type", "application/json;charset=utf-8");

        List<Map<String, Object>> datas = new ArrayList<>();
        for (VvSaSaleOrder vvSaSaleOrder : vvSaSaleOrdersList) {
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("app_id", "6500165a60a971000ab0ad28");
            parameters.put("entry_id", "65015e41ddd8340007a1698d");
            List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameters, originalHeaders, vvSaSaleOrder.getDocumentnumber(),"documentnumber");
            // 查询视图，这条数据是否存在
            int i1 = vvSaSaleOrderMapper.inquireIsExist(vvSaSaleOrder.getDocumentnumber());
            if (i1 == 0) {
                // 存入视图
                vvSaSaleOrderMapper.insertVVSaleOrder(vvSaSaleOrder);
                List<VvSaSaleOrderB> vvSaSaleOrderbList = vvSaSaleOrder.getVvsasaleorderblist();
                for (VvSaSaleOrderB vvSaSaleOrderB : vvSaSaleOrderbList) {
                    vvSaSaleOrderBMapper.insertSaleOrderDetail(vvSaSaleOrderB);
                }
            }
            // 查询是否已经存在简道云
            if (jdyPurchaseOrders.size() > 0) {
                continue;
            }
            datas.add( ConnmeUtils.convertEntityToMap(vvSaSaleOrder));
        }
        final int batchSize = 50;  // 每批处理的数据量
        int dataSize = datas.size();
        int numberOfBatches = dataSize % batchSize == 0 ? dataSize / batchSize : dataSize / batchSize + 1;

        for (int batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) {
            int startIndex = batchIndex * batchSize;
            int endIndex = Math.min(startIndex + batchSize, dataSize);

            List<Map<String, Object>> lista = datas.subList(startIndex, endIndex);

            Map<String, Object> parameter = new HashMap<>();
            parameter.put("app_id", "6500165a60a971000ab0ad28");
            parameter.put("entry_id", "65015e41ddd8340007a1698d");
            parameter.put("data_list", lista);

            String jsonSale = JSONUtil.toJsonStr(parameter);

            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/batch_create", originalHeaders, jsonSale);
        }
    }
}
